package com.yami.shop.nft.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.BlockchainShop;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.DataWebService;
import com.yami.shop.common.util.PageUtil;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.nft.common.dao.NftGasDeployMapper;
import com.yami.shop.nft.common.model.NftGasDeploy;
import com.yami.shop.nft.common.service.NftGasDeployService;
import com.yami.shop.service.BlockchainShopService;
import com.yami.shop.service.ShopDetailService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NftGasDeployServiceImpl extends ServiceImpl<NftGasDeployMapper, NftGasDeploy> implements NftGasDeployService {

    @Autowired
    private NftGasDeployMapper nftGasDeployMapper;

    @Autowired
    private BlockchainShopService blockchainShopService;

    @Autowired
    private DataWebService dataWebService;

    @Autowired
    private ShopConfig shopConfig;

    @Autowired
    private ShopDetailService shopDetailService;

    @Override
    public PageVO<NftGasDeploy> pageGasDeploy(PageDTO pageDTO, NftGasDeploy nftGasDeploy) {
        LambdaQueryWrapper<NftGasDeploy> nftGasDeployLambdaQueryWrapper = new LambdaQueryWrapper<>();
        nftGasDeployLambdaQueryWrapper.ne(NftGasDeploy::getStatus,-1);
        if(ObjectUtils.isNotEmpty(nftGasDeploy.getMchNo())){
            nftGasDeployLambdaQueryWrapper.eq(NftGasDeploy::getMchNo,nftGasDeploy.getMchNo());
        }
        PageVO<NftGasDeploy> page = PageUtil.doPage(pageDTO, () -> nftGasDeployMapper.selectList(nftGasDeployLambdaQueryWrapper));
        return page;
    }

    @Override
    public NftGasDeploy getGasListById(Integer id) {
        NftGasDeploy byId = this.getById(id);
        return byId;
    }

    @Override
    public void insertGasDeploy(NftGasDeploy nftGasDeploy) {
        if (ObjectUtils.isEmpty(nftGasDeploy.getNftOriginalPrice())) {
            throw new YamiShopBindException("单价不能为空");
        }
        nftGasDeploy.setCreateTime(new Date());
        nftGasDeploy.setStatus(0);
        nftGasDeploy.setNftOriginalPrice(nftGasDeploy.getNftOriginalPrice() * 100);
        nftGasDeploy.setNftSpecialPrice(nftGasDeploy.getNftSpecialPrice() * 100);
        save(nftGasDeploy);
    }

    @Override
    public void updateGasDeploy(NftGasDeploy nftGasDeploy) {
        nftGasDeploy.setUpdateTime(new Date());
        updateById(nftGasDeploy);
    }

    @Override
    public void deleteGasDeploy(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new YamiShopBindException("id不能为空");
        }
        NftGasDeploy nftGasDeploy = new NftGasDeploy();
        nftGasDeploy.setStatus(-1);
        nftGasDeploy.setId(id);
        updateById(nftGasDeploy);
    }

    @Override
    public void updateStatusGasDeploy(Integer id, Integer status) {
        NftGasDeploy nftGasDeploy = new NftGasDeploy();
        nftGasDeploy.setId(id);
        nftGasDeploy.setStatus(status);
        updateById(nftGasDeploy);
    }

    @Override
    public List<NftGasDeploy> getGasDeployList() {
        LambdaQueryWrapper<NftGasDeploy> nftGasDeployLambdaQueryWrapper = new LambdaQueryWrapper<>();
        nftGasDeployLambdaQueryWrapper.eq(NftGasDeploy::getStatus,1);
        nftGasDeployLambdaQueryWrapper.eq(NftGasDeploy::getMchNo,0);
        nftGasDeployLambdaQueryWrapper.orderByAsc(NftGasDeploy::getSort);
        List<NftGasDeploy> list = list(nftGasDeployLambdaQueryWrapper);
        return list;
    }

    @Override
    public HashMap<Object, Object> getShopGasDeployList(ShopDetail shopDetail, String chain) {
        HashMap<Object, Object> map = new HashMap<>();
        LambdaQueryWrapper<NftGasDeploy> nftGasDeployLambdaQueryWrapper = new LambdaQueryWrapper<>();
        nftGasDeployLambdaQueryWrapper.eq(NftGasDeploy::getStatus,1);
        nftGasDeployLambdaQueryWrapper.eq(NftGasDeploy::getMchNo,1);
        nftGasDeployLambdaQueryWrapper.orderByAsc(NftGasDeploy::getSort);
        List<NftGasDeploy> nftGasDeploys = list(nftGasDeployLambdaQueryWrapper);
        map.put("nftGasDeployList", nftGasDeploys);
//        LambdaQueryWrapper<BlockchainShop> eq = new LambdaQueryWrapper<BlockchainShop>().eq(BlockchainShop::getShopId, shopDetail.getShopId()).eq(BlockchainShop::getBlockchainCode, chain);
//        BlockchainShop one = blockchainShopService.getOne(eq);
        ShopDetail byId = shopDetailService.getById(shopDetail.getShopId());
        if(ObjectUtils.isEmpty(byId)){
            throw new YamiShopBindException("店铺折扣获取失败");
        }
        if(ObjectUtils.isEmpty(byId.getDiscount())){
            byId.setDiscount(0.0000);
        }
        map.put("discount", byId.getDiscount());
        Map<String, String> stringObjectMap3 = new HashMap<>();
        String url = shopConfig.getApiGatewayUrl()+"/nft/outapi/nftUser/getInfoForRomate";
        JSONObject jsonObject = dataWebService.hutoolPostWebService(url, stringObjectMap3,shopDetail.getApiUser(),shopDetail.getApiPassword());
        if(jsonObject.getInteger("code")== Constant.WARN){
            map.put("totalGas",0);
            map.put("code",0); //店铺暂无操作权限
        }else if(jsonObject.getInteger("code")== Constant.SUCCESS_CODE){
            JSONObject datainfo = jsonObject.getJSONObject("data");
            map.put("totalGas",datainfo.getString("totalTimes"));
            map.put("code",1); //店铺已开通权限
        }
        return map;
    }
}
